package com.apetrenko.avaya.callstore.server;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import com.apetrenko.avaya.callstore.client.Config;

public class ConfigStorage {

	private Preferences iPrefs;

	public static final int ARCHIVE_PERIOD_DEFAULT = 60;
	public static final String VRL_PATH_DEFAULT = "D:\\tmp\\vrl\\";
	public static final String ARCHIVE_PATH_DEFAULT = "D:\\tmp\\archive\\";
	private static final String DB_DRIVER_DEFAULT = "";
	private static final String DB_URL_DEFAULT = "";
	private static final String DB_USER_DEFAULT = "";
	private static final String DB_PASSWORD_DEFAULT = "";

	public void init(Class<?> aClass) {
		iPrefs = Preferences.systemNodeForPackage(this.getClass());
	}

	public Config load() {
		Config vResult = new Config();
		try {
			iPrefs.sync();
		} catch (BackingStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		vResult.setArchivePeriod(iPrefs.getInt(Config.ARCHIVE_PERIOD,
				ARCHIVE_PERIOD_DEFAULT));
		vResult.setVrlPath(iPrefs.get(Config.VRL_PATH, VRL_PATH_DEFAULT));
		vResult.setArchivePath(iPrefs.get(Config.ARCHIVE_PATH,
				ARCHIVE_PATH_DEFAULT));
		vResult.setDbDriver(iPrefs.get(Config.DB_DRIVER, DB_DRIVER_DEFAULT));
		vResult.setDbUrl(iPrefs.get(Config.DB_URL, DB_URL_DEFAULT));
		vResult.setDbUser(iPrefs.get(Config.DB_USER, DB_USER_DEFAULT));
		vResult.setDbPassword(iPrefs.get(Config.DB_PASSWORD,
				DB_PASSWORD_DEFAULT));
		return vResult;
	}

	public void store(Config aConfig) {
		iPrefs.putInt(Config.ARCHIVE_PERIOD, aConfig.getArchivePeriod());
		iPrefs.put(Config.VRL_PATH, aConfig.getVrlPath());
		iPrefs.put(Config.ARCHIVE_PATH, aConfig.getArchivePath());
		try {
			iPrefs.flush();
		} catch (BackingStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
